﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace MassMailing.WPF.Settings
{
    /// <summary>
    /// The settings for the entire mailing application
    /// </summary>
    [Serializable]
    public class MailSettings
    {
        /// <summary>
        /// The smtp settings for the mass mailer settings.
        /// </summary>
        public List<SMTPSetting> SMTPSettings { get; set; }

        #region Methods

        /// <summary>
        /// Saves the settings to the file system.
        /// </summary>
        /// <param name="mailSettings">The mail settings to save.</param>
        public static void SaveSettings(MailSettings mailSettings)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(MailSettings));
            using (FileStream stream = File.Open("MailSettings.xml", FileMode.Create))
            {
                serializer.Serialize(stream, mailSettings);
            }
        }

        /// <summary>
        /// Loads the mail settings from the file system then returns it.
        /// </summary>
        /// <returns>The mail settings from the file system if it exists.</returns>
        public static MailSettings LoadSettings()
        {
            MailSettings result = null;
            if (File.Exists("MailSettings.xml"))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MailSettings));
                using (FileStream stream = File.Open("MailSettings.xml", FileMode.Open))
                {
                    result = serializer.Deserialize(stream) as MailSettings;
                }
            }

            if (result == null)
            {
                result = new MailSettings();
                result.SMTPSettings = new List<SMTPSetting>();
            }

            return result;
        }

        #endregion
    }
}
